package com.zhpan.indicator.drawer;

import ohos.agp.render.Paint;

import com.zhpan.indicator.base.MeasureResult;
import com.zhpan.indicator.enums.IndicatorOrientation;
import com.zhpan.indicator.options.IndicatorOptions;

/**
 * 形状基类
 *
 * @since 2021-04-13
 */
public abstract class BaseDrawer implements IDrawer {
    protected static final int INDICATOR_PADDING_ADDITION = 6;
    protected static final int INDICATOR_PADDING = 3;

    protected float maxWidth;
    protected float minWidth;

    protected Paint mPaint = new Paint();
    protected IndicatorOptions mOptions;
    private MeasureResult mMeasureResult;

    /**
     * 根据配置信息创建形状
     *
     * @param options 配置信息
     */
    public BaseDrawer(IndicatorOptions options) {
        this.mOptions = options;
        mMeasureResult = new MeasureResult();
    }

    @Override
    public MeasureResult onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        maxWidth = coerceAtLeast(mOptions.getNormalSliderWidth(), mOptions.getCheckedSliderWidth());
        minWidth = coerceAtMost(mOptions.getNormalSliderWidth(), mOptions.getCheckedSliderWidth());

        int orientation = mOptions.getOrientation();
        if (orientation == IndicatorOrientation.INDICATOR_VERTICAL) {
            mMeasureResult.setMeasureResult(measureHeight(), measureWidth());
        } else {
            mMeasureResult.setMeasureResult(measureWidth(), measureHeight());
        }
        return mMeasureResult;
    }

    /**
     * 布局测量
     *
     * @param changed 是否改变
     * @param left 左坐标
     * @param top 上坐标
     * @param right 右坐标
     * @param bottom 下坐标
     */
    @Override
    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
    }

    /**
     * 获取测量搞得
     *
     * @return 高度
     */
    protected int measureHeight() {
        return (int) (mOptions.getSliderHeight() + INDICATOR_PADDING);
    }

    /**
     * 获取测量宽度
     *
     * @return 宽度
     */
    protected int measureWidth() {
        int pageSize = mOptions.getPageSize();
        float sliderGap = mOptions.getSliderGap();
        return (int) (((pageSize - 1) * sliderGap + maxWidth + (pageSize - 1) * minWidth) + INDICATOR_PADDING_ADDITION);
    }

    /**
     * 宽高是否相等
     *
     * @return true 相等 false 不相等
     */
    protected boolean isWidthEquals() {
        return mOptions.getNormalSliderWidth() == mOptions.getCheckedSliderWidth();
    }

    /**
     * 比较发小，返回大的
     *
     * @param self 当前值
     * @param minimumValue 比较的值
     * @return 两者更大值
     */
    protected float coerceAtLeast(float self, float minimumValue) {
        if (self < minimumValue) {
            return minimumValue;
        } else {
            return self;
        }
    }

    /**
     * 返回更小值
     *
     * @param self 当前值
     * @param maximumValue 比较的值
     * @return 返回更小的值
     */
    protected float coerceAtMost(float self, float maximumValue) {
        if (self > maximumValue) {
            return maximumValue;
        } else {
            return self;
        }
    }
}
